﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interactivity;
using System.Windows.Media;

namespace GameLogic.Logic
{
    public class PathNavigationBehavior : GameContainerBehavior<FrameworkElement>
    {
        private MovingCollidableBehavior movableCollidableBehavior;
        private bool isTracking;
        private List<Point> path;
        private PathGeometry pathGeometry;

        protected override void OnAttached()
        {
            base.OnAttached();
            this.AssociatedObject.Loaded += this.OnAssociatedObjectLoaded;
        }

        protected override void OnGameContainerInitialized()
        {
            base.OnGameContainerInitialized();
            //this.GameContainer.GameLoop += this.OnGameContainer_GameLoop;
        }

        private void OnAssociatedObjectLoaded(object sender, RoutedEventArgs e)
        {
            // Get required MovableCollidableBehavior
            foreach (Behavior behavior in Interaction.GetBehaviors(this.AssociatedObject))
            {
                if (behavior is MovingCollidableBehavior)
                {
                    this.movableCollidableBehavior = (MovingCollidableBehavior)behavior;
                    break;
                }
            }

            this.AssociatedObject.MouseLeftButtonDown += this.OnAssociatedObject_MouseLeftButtonDown;
            this.AssociatedObject.MouseLeftButtonUp += this.OnAssociatedObject_MouseLeftButtonUp;
        }

        // If Mouse Down, start collecting path information
        private void OnAssociatedObject_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.path = new List<Point>();
            this.path.Add((e.GetPosition(this.AssociatedObject)));
            this.StartTracking();
        }

        // If Mouse Up, start collecting path information
        private void OnAssociatedObject_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            this.StopTracking();
        }

        void AssociatedElement_MouseMove(object sender, MouseEventArgs e)
        {
            this.path.Add((e.GetPosition(this.AssociatedObject)));

            //TEST: Drawp points
               //this.GameContainer.GameControl 

            //PathFigure pathFigure = new PathFigure;
            //pathFigure.StartPoint = e.GetPosition(this.AssociatedObject);

            //for (int i = 0; i <= this.path.Count; i++)
            //{
            //    PathSegment pathSegment = new PathSegment();
            //    pathSegment.
            //    pathFigure.Segments.Add(new PathSegment());
            //}

            //this.pathGeometry = new PathGeometry();
        }

        void OnAssociatedObject_LostMouseCapture(object sender, EventArgs e)
        {
            this.isTracking = false;
            this.AssociatedObject.MouseMove -= this.AssociatedElement_MouseMove;
            this.AssociatedObject.MouseLeftButtonUp -= this.OnAssociatedObject_MouseLeftButtonUp;
            this.AssociatedObject.LostMouseCapture -= this.OnAssociatedObject_LostMouseCapture;

            //if (this.path.Count > 0)
            //{

            //    double dx = (movableCollidableBehavior.X - this.path[0].X);
            //    double dy = (movableCollidableBehavior.Y - this.path[0].Y);

            //    Vector2D v = new Vector2D(dx, dy);
            //    v.Normalize();

            //    this.movableCollidableBehavior.Velocity = v;
            //}
        }

        private void StartTracking()
        {
            if (this.AssociatedObject.CaptureMouse())
            {
                this.isTracking = true;
                this.AssociatedObject.MouseMove += this.AssociatedElement_MouseMove;
                this.AssociatedObject.MouseLeftButtonUp += this.OnAssociatedObject_MouseLeftButtonUp;
                this.AssociatedObject.LostMouseCapture += this.OnAssociatedObject_LostMouseCapture;
            }
        }

        private void StopTracking()
        {
            this.AssociatedObject.ReleaseMouseCapture();
        }

        private void OnGameContainer_GameLoop(object sender, EventArgs e)
        {
            if (!this.isTracking)
            {
                //TODO: set element movement
            }
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();
            this.AssociatedObject.Loaded -= this.OnAssociatedObjectLoaded;
            this.AssociatedObject.MouseLeftButtonDown -= this.OnAssociatedObject_MouseLeftButtonDown;
            if (this.isTracking)
            {
                this.StopTracking();
            }
        }
    }
}
